//
//  BannerView.swift
//  casdon_swiftUI
//
//  Created by ww on 2025/5/2.
//
import SwiftUI
import Combine

struct BannerView: View {
    // 图片的 URL 数组，外部传入
    let bannerItems: [HomeAPI.Banner.Data]
    
    // 是否启用自动滑动（默认启用），自动滑动间隔
    var autoScroll: Bool = true
    var scrollInterval: TimeInterval = 3.0
    
    // 自定义样式：当前指示器颜色，未选中时的颜色
    var indicatorActiveColor: Color = AppColors.Gy3
    var indicatorInactiveColor: Color = .gray
    
    // 点击和长按的回调
    var onBannerTap: ((HomeAPI.Banner.Data) -> Void)?
    var onBannerLongPress: ((HomeAPI.Banner.Data) -> Void)?
    
    @State private var currentIndex: Int = 0
    private var timer: Publishers.Autoconnect<Timer.TimerPublisher>?
    
    init(
        bannerItems: [HomeAPI.Banner.Data],
        autoScroll: Bool = true,
        scrollInterval: TimeInterval = 3.0,
        indicatorActiveColor: Color = .blue,
        indicatorInactiveColor: Color = .gray,
        onBannerTap: ((HomeAPI.Banner.Data) -> Void)? = nil,
        onBannerLongPress: ((HomeAPI.Banner.Data) -> Void)? = nil
    ) {
        self.bannerItems = bannerItems
        self.autoScroll = autoScroll
        self.scrollInterval = scrollInterval
        self.indicatorActiveColor = indicatorActiveColor
        self.indicatorInactiveColor = indicatorInactiveColor
        self.onBannerTap = onBannerTap
        self.onBannerLongPress = onBannerLongPress
        
        if autoScroll {
            self.timer = Timer.publish(every: scrollInterval, on: .main, in: .common).autoconnect()
        }
    }
    
    var body: some View {
        ZStack {
            // Banner滑动视图
            TabView(selection: $currentIndex) {
                ForEach(0..<bannerItems.count, id: \.self) { index in
                    // 使用 `TapGesture` 和 `LongPressGesture` 来处理点击和长按事件
                    AsyncImage(url: URL(string: bannerItems[index].picture)) { image in
                        image.resizable()
                             .scaledToFill()
                             .frame(height: 148)
                             .clipped()
                             .gesture(TapGesture().onEnded {
                                 // 点击事件
                                 onBannerTap?(bannerItems[index])
                             })
                             .gesture(LongPressGesture().onEnded { _ in
                                 // 长按事件
                                 onBannerLongPress?(bannerItems[index])
                             })
                    } placeholder: {
                        ProgressView()
                            .frame(height: 148)
                    }
                    .tag(index)
                }
            }
            .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never)) // 禁用默认分页指示器
            VStack {
                Spacer()
                // 自定义分页指示器
                HStack(spacing: 8) {
                    ForEach(0..<bannerItems.count, id: \.self) { index in
                        Circle()
                            .fill(currentIndex == index ? indicatorActiveColor : indicatorInactiveColor)
                            .frame(width: 8, height: 8)
                            .padding(.bottom, -8)
                    }
                }
            }
        }
        .frame(height: 148)
        .cornerRadius(12)
        .padding(.horizontal)
        .onReceive(timer ?? Timer.publish(every: scrollInterval, on: .main, in: .common).autoconnect()) { _ in
            withAnimation {
                if bannerItems.count > 0{
                    currentIndex = (currentIndex + 1) % bannerItems.count
                }
            }
        }
    }
}



